package distributed.controller;

import org.w3c.dom.NamedNodeMap;

import distributed.entity.Event;
import distributed.entity.EventModel;
import distributed.entity.User;

import server.ClientState;
import server.Server;
import xml.Message;

/**
 * Controller to handle the closing of a specific event
 * @author Marcus Menghini, Sam Jaffe
 *
 */
public class CloseEventController implements IProtocolController{

	/** Processes a message from a client */
	public Message process(ClientState state, Message request) {
		String xmlString;
		//Get the request attributes
		NamedNodeMap map = request.contents.getFirstChild().getAttributes();
		//Get the event id
		String id = map.getNamedItem("id").getNodeValue();
		// Get the event
		Event event = EventModel.getInstance().getEvent(id);
		//Close it
		event.close();
		
		xmlString = Message.responseHeader(request.id()) + "<closeResponse/></response>";
		
		Message send = new Message(xmlString);
		//Let everyone know we're closed
		for (User u : event.getUsers())
			if (u != null)
				Server.getState(u.getID()).sendMessage(send);
		
		send = new Message(Message.responseHeader(request.id()) + "<turnResponse/></response>");
		//Tell the mod that it's his turn
		return send;
	}

	/**
	 * CloseEventController
	 */
	public IProtocolController newCopy() {
		return new CloseEventController();
	}

}
